<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>



    <script>
        function formate(url, data) {
            let type = "get";
            if (arguments.length === 1) {
                type = "post";
                data = url;
                url = "";
            }
            let start = true;
            for (let key in data) {
                if (type === "get") {
                    url += (start ? "?" : "&") + key + "=" + data[key];
                } else if (type === "post") {
                    url += (start ? "" : "&") + key + "=" + data[key];
                }
                start = false;
            }
            return url;
        }

        function ajax(options) {
            options = Object.assign({
                options: "get",
                callback: function () { },
                url: "",
                data: {},
                jsoncallback: "callback"
            }, options);

            if (options.method === "jsonp") {
                let script = document.createElement("script");
                script.src = formate(options.url, options.data);
                document.body.appendChild(script);
                window[options.jsoncallback] = (data) => {
                    options.callback(data);
                }
                script.onload = () => {
                    script.remove()
                }
            } else {
                let xhr = new XMLHttpRequest();
                xhr.open(options.method, options.method.toLowerCase() === "get" ? formate(options.url, options.data) : options.url);
                if (options.method.toLowerCase() === "post") {
                    xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
                }
                xhr.send(options.method.toLowerCase() === "post" ? formate(options.data) : null);
                xhr.onreadystatechange = () => {
                    if (xhr.readyState === 4 && /^2\d{2}$/.test(xhr.status)) {
                        options.callback(xhr.responseText)
                    }
                }
            }
        }

        ajax({
            method: "jsonp",
            url: "https://api.gogoup.com/p1/data/recommend",
            data: {
                utm_term: "lb"
            },
            jsoncallback: "callback",
            callback: function (data) {
                // console.log("a")
                console.log(data)
            }
        })


        // ajax({
        //     method: "jsonp",
        //     url: "https://www.baidu.com/sugrec",
        //     data: {
        //         pre: 1,
        //         p: 3,
        //         ie: "utf-8",
        //         json: 1,
        //         prod: "pc",
        //         from: "pc_web",
        //         sugsid: "32218,1425,31672,32139,31254,32045,32230,32299,31639",
        //         wd: "hello world",
        //         req: 2,
        //         csor: 5,
        //         cb: "callback",
        //         _: Date.now()
        //     },
        //     jsonpcallback: "callback",
        //     callback: function (data) {
        //         console.log(data);
        //     }
        // })
    </script>
</body>


</html>